package adv9;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
public class XML {
    public static void main(String[] args) {
        //读，删除，增加，修改
        read();
    }

    public static void read() {
        SAXReader reader = new SAXReader();
        try {
            Document doc = reader.read(new File("E:\\java_workspace\\IdeaProjects\\java86\\javaAdvance\\src\\adv9" +
                    "\\book.xml"));
            Element root = doc.getRootElement();
            System.out.println(root.getName());
            //books下面
            List<Node> books = root.selectNodes("book");
            for (Node n : books) {
                Element e = (Element) n;
                String index = e.attributeValue("index");
                String diff = e.attributeValue("diff");
                String name = e.selectSingleNode("name").getText();
                String price = e.selectSingleNode("price").getText();
                double money =Double.parseDouble(price);
                String desc = e.selectSingleNode("desc").getText().trim();

                //System.out.printf("index=%s,难度%s,名称：%s,价格 %e,描述=%s",index,diff,name,price,desc);
                System.out.printf("index="+index+",难度"+diff+",名称："+name+",价格 "+money+",描述"+desc+"\n");
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }


    }
}
